{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2f05a5fa-0b5b-4c36-9ce6-595e7f0f63f8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] <class 'list'>\n",
      "[2, 3, 4, 5, 6, 7] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']\n",
    "print(x, type(x))\n",
    "\n",
    "x = [2, 3, 4, 5, 6, 7]\n",
    "print(x, type(x))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "28e7bf1b-d9c3-473d-90b3-7f6ecd15668f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] <class 'list'>\n",
      "[1, 3, 5, 7, 9] <class 'list'>\n",
      "[10, 8, 6, 4, 2] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "x = list(range(10))\n",
    "print(x, type(x))\n",
    "\n",
    "\n",
    "x = list(range(1, 11, 2))\n",
    "print(x, type(x))\n",
    "\n",
    "\n",
    "x = list(range(10, 1, -2))\n",
    "print(x, type(x))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fefa0c0b-d77c-4264-b955-25c3534a92c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 0, 0, 0, 0] <class 'list'>\n",
      "[0, 0, 0, 0, 0] <class 'list'>\n",
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] <class 'list'>\n",
      "[1, 3, 5, 7, 9] <class 'list'>\n",
      "[10, 8, 6, 4, 2] <class 'list'>\n",
      "[1, 4, 9, 16, 25, 36, 49, 64, 81] <class 'list'>\n",
      "[3, 9, 15, 21, 27, 33, 39, 45, 51, 57, 63, 69, 75, 81, 87, 93, 99] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "x = [0] * 5\n",
    "print(x, type(x))\n",
    "# [0, 0, 0, 0, 0] <class 'list'>\n",
    "\n",
    "x = [0 for i in range(5)]\n",
    "print(x, type(x))\n",
    "# [0, 0, 0, 0, 0] <class 'list'>\n",
    "\n",
    "x = [i for i in range(10)]\n",
    "print(x, type(x))\n",
    "# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] <class 'list'>\n",
    "\n",
    "x = [i for i in range(1, 10, 2)]\n",
    "print(x, type(x))\n",
    "# [1, 3, 5, 7, 9] <class 'list'>\n",
    "\n",
    "x = [i for i in range(10, 1, -2)]\n",
    "print(x, type(x))\n",
    "# [10, 8, 6, 4, 2] <class 'list'>\n",
    "\n",
    "x = [i ** 2 for i in range(1, 10)]\n",
    "print(x, type(x))\n",
    "# [1, 4, 9, 16, 25, 36, 49, 64, 81] <class 'list'>\n",
    "\n",
    "x = [i for i in range(100) if (i % 2) != 0 and (i % 3) == 0]\n",
    "print(x, type(x))\n",
    "\n",
    "# [3, 9, 15, 21, 27, 33, 39,"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "96c332a6-2aed-4e7e-b5ac-aeeca7bd82a9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]] <class 'list'>\n",
      "[[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0]] <class 'list'>\n",
      "[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]] <class 'list'>\n",
      "[[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0]] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "x = [[0] * 3] * 4\n",
    "print(x, type(x))\n",
    "# [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]] <class 'list'>\n",
    "\n",
    "x[0][0] = 1\n",
    "print(x, type(x))\n",
    "# [[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0]] <class 'list'>\n",
    "\n",
    "a = [0] * 3\n",
    "x = [a] * 4\n",
    "print(x, type(x))\n",
    "# [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]] <class 'list'>\n",
    "\n",
    "x[0][0] = 1\n",
    "print(x, type(x))\n",
    "# [[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0]] <class 'list'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "aa859ee6-d180-46e6-9b3a-bac9cb8bbdc5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 'lsgo', 3.14, [1, 2, 3]] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "mix = [1, 'lsgo', 3.14, [1, 2, 3]]\n",
    "print(mix, type(mix))  \n",
    "# [1, 'lsgo', 3.14, [1, 2, 3]] <class 'list'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7d72cbd2-bfcb-4393-9d25-982fad06e782",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "empty = []\n",
    "print(empty, type(empty))  # [] <class 'list'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3b0ab520-2ae1-4d91-9c98-6bc7e8e33cc6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']\n",
    "x.append('Saturday')\n",
    "print(x)  \n",
    "print(len(x))  # 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4b0113ed-aa7c-4872-86f6-e7762c5fe55b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', ['Saturday', 'Sunday']]\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']\n",
    "x.append(['Saturday', 'Sunday'])\n",
    "print(x)  \n",
    "print(len(x))  # 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f216184d-9db6-486c-adcb-7306fd79ba92",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']\n",
    "x.extend(['Saturday', 'Sunday'])\n",
    "print(x)  \n",
    "\n",
    "print(len(x))  # 7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "89ba351a-9ebb-4001-8f26-a9ff694dcc95",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Monday', 'Tuesday', 'Sunday', 'Wednesday', 'Thursday', 'Friday']\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']\n",
    "x.insert(2, 'Sunday')\n",
    "print(x)\n",
    "\n",
    "print(len(x))  # 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5db4c4c1-d510-4bb5-bb01-227d2efcbed7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Tuesday', 'Wednesday', 'Thursday', 'Friday']\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']\n",
    "x.remove('Monday')\n",
    "print(x)  \n",
    "print(len(x))  # 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "7951e397-7798-4066-8c67-5c8ad9b36f76",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Friday\n",
      "Monday\n",
      "Wednesday\n",
      "['Tuesday', 'Thursday']\n"
     ]
    }
   ],
   "source": [
    "x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']\n",
    "y = x.pop()\n",
    "print(y)  # Friday\n",
    "\n",
    "y = x.pop(0)\n",
    "print(y)  # Monday\n",
    "\n",
    "y = x.pop(-2)\n",
    "print(y)  # Wednesday\n",
    "print(x)  # ['Tuesday', 'Thursday']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "a250a3f4-4a57-4d02-8828-186876f3ddb7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Monday', 'Thursday', 'Friday']\n"
     ]
    }
   ],
   "source": [
    "x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']\n",
    "del x[1:3] #从下标1个开始，删除到下标3\n",
    "print(x) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "aee019e5-27ea-49df-a44d-a9e77267b2e8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Monday <class 'str'>\n",
      "['Thursday', 'Friday'] <class 'list'>\n",
      "Wednesday <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "#通过元素的索引值，从列表获取单个元素，注意，列表索引值是从0开始的。\n",
    "#通过将索引指定为-1，可让Python返回最后一个列表元素，索引 -2 返回倒数第二个列表元素，以此类推。\n",
    "\n",
    "x = ['Monday', 'Tuesday', 'Wednesday', ['Thursday', 'Friday']]\n",
    "print(x[0], type(x[0]))  # Monday <class 'str'>\n",
    "print(x[-1], type(x[-1]))  # ['Thursday', 'Friday'] <class 'list'>\n",
    "print(x[-2], type(x[-2]))  # Wednesday <class 'str'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "3d046d4b-d487-4e0e-8a74-e98dc9e88dfe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Thursday', 'Friday']\n",
      "['Wednesday', 'Thursday', 'Friday']\n"
     ]
    }
   ],
   "source": [
    "x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']\n",
    "print(x[3:])  # ['Thursday', 'Friday']\n",
    "print(x[-3:])  # ['Wednesday', 'Thursday', 'Friday']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "bd9dc44e-7805-4cbd-85fe-bc6ab0b49500",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Monday', 'Tuesday', 'Wednesday']\n",
      "['Monday', 'Tuesday']\n"
     ]
    }
   ],
   "source": [
    "week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']\n",
    "print(week[:3])  # ['Monday', 'Tuesday', 'Wednesday']\n",
    "print(week[:-3])  # ['Monday', 'Tuesday']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "ff29e7b0-29a4-4f9c-9dc2-3ab1929f0b6a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Tuesday', 'Wednesday']\n",
      "['Wednesday', 'Thursday']\n"
     ]
    }
   ],
   "source": [
    "week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']\n",
    "print(week[1:3])  # ['Tuesday', 'Wednesday']\n",
    "print(week[-3:-1])  # ['Wednesday', 'Thursday']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "44c70a43-adc5-4b0e-8a26-3d2c6a02de24",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Tuesday', 'Thursday']\n",
      "['Monday', 'Wednesday']\n",
      "['Tuesday', 'Thursday']\n",
      "['Friday', 'Thursday', 'Wednesday', 'Tuesday', 'Monday']\n"
     ]
    }
   ],
   "source": [
    "#以具体的 step 从编号 start 往编号 stop 切片。注意最后把 step 设为 -1，相当于将列表反向排列。\n",
    "week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']\n",
    "print(week[1:4:2])  # ['Tuesday', 'Thursday']\n",
    "print(week[:4:2])  # ['Monday', 'Wednesday']\n",
    "print(week[1::2])  # ['Tuesday', 'Thursday']\n",
    "print(week[::-1])  \n",
    "# ['Friday', 'Thursday', 'Wednesday', 'Tuesday', 'Monday']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "71838994-96df-4cd7-a573-a78d0101fda5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']\n"
     ]
    }
   ],
   "source": [
    "#复制列表中的所有元素（浅拷贝）\n",
    "week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']\n",
    "print(week[:])  \n",
    "# ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "c7f0639f-5438-4d7f-be2d-51fa0113e3a0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[123, 456, 789, 213]\n",
      "[123, 456, 789, 213]\n",
      "[123, 213, 456, 789]\n",
      "[123, 456, 789, 213]\n",
      "[111, 213, 456, 789]\n",
      "[123, 456, 789, 213]\n",
      "[[123, 456], [789, 213]]\n",
      "[[123, 456], [789, 213]]\n",
      "[[111, 456], [789, 213]]\n",
      "[[111, 456], [789, 213]]\n"
     ]
    }
   ],
   "source": [
    "#浅拷贝\n",
    "list1 = [123, 456, 789, 213]\n",
    "list2 = list1\n",
    "list3 = list1[:]\n",
    "\n",
    "print(list2)  # [123, 456, 789, 213]\n",
    "print(list3)  # [123, 456, 789, 213]\n",
    "list1.sort()\n",
    "print(list2)  # [123, 213, 456, 789] \n",
    "print(list3)  # [123, 456, 789, 213]\n",
    "list1[0] = 111\n",
    "print(list2)  # [111, 213, 456, 789] \n",
    "print(list3)  # [123, 456, 789, 213]\n",
    "list1 = [[123, 456], [789, 213]]\n",
    "list2 = list1\n",
    "list3 = list1[:]\n",
    "print(list2)  # [[123, 456], [789, 213]]\n",
    "print(list3)  # [[123, 456], [789, 213]]\n",
    "list1[0][0] = 111\n",
    "print(list2)  # [[111, 456], [789, 213]]\n",
    "print(list3)  # [[111, 456], [789, 213]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "289b648a-c25d-473e-88ef-46f6a36982f0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n",
      "[123, 456, 456, 123]\n",
      "[123, 456, 123, 456, 123, 456]\n",
      "[123, 456, 123, 456, 123, 456]\n",
      "[123, 456, 123, 456, 123, 456]\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "list1 = [123, 456]\n",
    "list2 = [456, 123]\n",
    "list3 = [123, 456]\n",
    "\n",
    "print(list1 == list2)  # False\n",
    "print(list1 == list3)  # True\n",
    "\n",
    "list4 = list1 + list2  # extend()\n",
    "print(list4)  # [123, 456, 456, 123]\n",
    "\n",
    "list5 = list3 * 3\n",
    "print(list5)  # [123, 456, 123, 456, 123, 456]\n",
    "\n",
    "list3 *= 3\n",
    "print(list3)  # [123, 456, 123, 456, 123, 456]\n",
    "print(list5)  # [123, 456, 123, 456, 123, 456]\n",
    "\n",
    "print(123 in list3)  # True\n",
    "print(456 not in list3)  # False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "e9bd3b6f-4f24-41ec-83d7-3b2935967fed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[123, 456, 123, 456, 123, 456]\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "# list.count(obj) 统计某个元素在列表中出现的次数\n",
    "list1 = [123, 456] * 3\n",
    "print(list1)  # [123, 456, 123, 456, 123, 456]\n",
    "num = list1.count(123)\n",
    "print(num)  # 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "1259e874-d249-4f18-86cc-24382695256e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[123, 456, 123, 456, 123, 456, 123, 456, 123, 456]\n",
      "0\n",
      "2\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "# list.index(x[, start[, end]]) 从列表中找出某个值第一个匹配项的索引位置\n",
    "list1 = [123, 456] * 5\n",
    "print(list1) \n",
    "print(list1.index(123))  # 0\n",
    "print(list1.index(123, 1))  # 2\n",
    "print(list1.index(123, 3, 7))  # 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "4d344ceb-b853-4bb1-bbdd-314475438abc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[789, 456, 123]\n"
     ]
    }
   ],
   "source": [
    "#list.reverse() 反向列表中元素\n",
    "x = [123, 456, 789]\n",
    "x.reverse()\n",
    "print(x)  # [789, 456, 123]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "6e6b138f-493e-4460-8d3f-95c033f807cd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[123, 213, 456, 789]\n",
      "[789, 456, 213, 123]\n",
      "[(4, 1), (2, 2), (1, 3), (3, 4)]\n",
      "[(1, 3), (2, 2), (3, 4), (4, 1)]\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "list.sort(key=None, reverse=False) 对原列表进行排序。\n",
    "key -- 主要是用来进行比较的元素，只有一个参数，具体的函数的参数就是取自于可迭代对象中，指定可迭代对象中的一个元素来进行排序。\n",
    "reverse -- 排序规则，reverse = True 降序， reverse = False 升序（默认）。\n",
    "该方法没有返回值，但是会对列表的对象进行排序。\n",
    "'''\n",
    "\n",
    "x = [123, 456, 789, 213]\n",
    "x.sort()\n",
    "print(x)\n",
    "# [123, 213, 456, 789]\n",
    "\n",
    "x.sort(reverse=True)\n",
    "print(x)\n",
    "# [789, 456, 213, 123]\n",
    "\n",
    "\n",
    "# 获取列表的第二个元素\n",
    "def takeSecond(elem):\n",
    "    return elem[1]\n",
    "\n",
    "\n",
    "x = [(2, 2), (3, 4), (4, 1), (1, 3)]\n",
    "x.sort(key=takeSecond)\n",
    "print(x)\n",
    "# [(4, 1), (2, 2), (1, 3), (3, 4)]\n",
    "\n",
    "x.sort(key=lambda a: a[0])\n",
    "print(x)\n",
    "# [(1, 3), (2, 2), (3, 4), (4, 1)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d921cfe3-ad43-4723-9410-3e1ad9f352f3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.23"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
